package AutoTests;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class Tests extends InitAndEnd{
     @ParameterizedTest
    @CsvSource({"'http://localhost:8080/blog_system/login.html', 'lisi', 'http://localhost:8080/blog_system/blog_list.html', '123'"})
    void login(String url, String username, String expected_url, String password) throws InterruptedException {
         webDriver.get(url);
         Thread.sleep(3000);
         webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
         webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
         webDriver.findElement(By.cssSelector("#submit")).click();
         Thread.sleep(3000);
         String cur_username = webDriver.findElement(By.cssSelector("body > div.container > div.container-left > div > h3")).getText();
         if(cur_username.equals(username)) {
             System.out.println("测试通过");
         }else {
             System.out.println("测试不通过");
         }
         String currentUrl = webDriver.getCurrentUrl();
         if(currentUrl.equals(expected_url)) {
             System.out.println("测试通过");
         }else {
             System.out.println("测试不通过");
         }
     }

     @Test
    void loginError() throws InterruptedException {
       String username = "lisi";
       String password = "1234";
       webDriver.get("http://localhost:8080/blog_system/login.html");
       Thread.sleep(3000);
         webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
         webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
         webDriver.findElement(By.cssSelector("#submit")).click();
         Thread.sleep(3000);
         WebElement webElement = webDriver.findElement(By.cssSelector("body"));
//         System.out.println(webElement.getText());
         Assertions.assertEquals("您输入的用户名或密码不正确！", webElement.getText());
     }
}
